<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/index.css">
    <script src="js/vue.min.js"></script>
    <script src="js/element.js"></script>
    <style>
        body, html {
            height: 100%;
            margin: 0;
            overflow-x: hidden;
        }

        .background {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
            background-image: url('img/bg.jpg');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
        }

        #box {
            width: 500px;
            height: 620px;
            background: white;
            border-radius: 5px;
            margin: 100px auto;
        }
    </style>
    <link rel="stylesheet" href="css/element.css">
</head>

<body>
<!--背景图-->
<div class="background"></div>
<div id="box">
    <div class="">
        <h1 style="padding-top: 50px;text-align: center">新用户注册</h1>
        <p style="text-align: center"> 请输入您的信息：</p>
    </div>
    <!--    表单-->
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="30px" class="demo-ruleForm"
             style="width: 445px;">

        <!--        邮箱-->
        <el-form-item prop="email">
            <el-input type="email" v-model="ruleForm.email" autocomplete="off" placeholder="邮箱"></el-input>
        </el-form-item>

        <!--        //用户名-->
        <el-form-item prop="username">
            <el-input type="text" v-model="ruleForm.username" autocomplete="off" placeholder="用户名"></el-input>
        </el-form-item>

        <!--        密码-->
        <el-form-item prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off" placeholder="密码"></el-input>
        </el-form-item>

        <!--        确认密码-->
        <el-form-item prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="确认密码"></el-input>
        </el-form-item>

        <!--        发送邮箱-->
        <el-form-item>
            <el-button :plain="false" type="primary" @click="open">发送邮箱</el-button>
        </el-form-item>

        <el-form-item>
            <el-button type="success" @click="submitForm(ruleForm)" style="width: 420px;">注册</el-button>
        </el-form-item>

        <el-form-item>
            <el-col :span="24">
                <el-link href="index.html" type="primary">返回登陆?</el-link>
            </el-col>
        </el-form-item>

    </el-form>
    <!--    表单-->

    <!-- 自定义对话框组件 -->
    <el-dialog
            :visible.sync="dialogVisible"
            title="发送邮箱"
            :show-close="false"
            :close-on-click-modal="false"
            :close-on-press-escape="false">
        <span>{{ dialogMessage }}</span>
        <template #footer>
                <span class="dialog-footer">
                    <el-button v-if="showConfirmButton" @click="dialogVisible = false">确定</el-button>
                </span>
        </template>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#box",
        data() {
            // 用户名
            var validateUserName = (rule, value, callback) => {
                var pattern = /^[\u3220-\uFA29]+$/
                if (value === '') {
                    callback(new Error('请输入用户名'));
                } else {
                    if (pattern.test(value)) {
                        this.$refs.ruleForm.validateField('');
                        callback();
                    } else {
                        callback(new Error('用户名格式错误，必须是中文'));
                    }
                }
            };

            // 密码
            var validatePass = (rule, value, callback) => {
                var pattern = /^\d{6,}$/
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (pattern.test(value)) {
                        this.$refs.ruleForm.validateField('');
                        callback();
                    } else {
                        callback(new Error('密码格式错误，六位数字以上'));
                    }
                }
            };
            // 邮箱
            var validateEmail = (rule, value, callback) => {
                // 使用更通用的邮箱正则表达式
                var pattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
                if (value === '') {
                    callback(new Error('请输入邮箱'));
                } else if (pattern.test(value)) {
                    callback(); // 验证通过
                } else {
                    callback(new Error('邮箱格式错误，格式为xxx@xx.xxx'));
                }
            }
            // 确认密码
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value!== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };

            return {
                ruleForm: {
                    username: "",
                    pass: '',
                    email: "",
                    checkPass: ""
                },
                rules: {
                    username: [
                        { validator: validateUserName, trigger: 'blur' }
                    ],
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    email: [
                        { validator: validateEmail, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ]
                },
                // 自定义对话框相关数据
                dialogVisible: false,
                dialogMessage: '',
                showConfirmButton: false
            };
        },
        methods: {
            submitForm() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        alert('注册成功！');
                    } else {
                        console.log('注册失败');
                return false;
            }
            });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            open: function () {
                if (this.ruleForm.email === '') {
                    this.$message.error('请输入邮箱地址');
                } else {
                    // 显示对话框
                    this.dialogMessage = '正在发送激活邮件到'+this.ruleForm.email+'';
                    this.dialogVisible = true;
                    this.showConfirmButton = false;

                    // 1秒后更新内容
                    setTimeout(() => {
                        this.dialogMessage = '激活邮件已发送，请检查您的邮箱';
                        this.showConfirmButton = true;
                    alert(a)
                }, 3000);
                    this.ruleForm.email = "";
                }
            }
        }
    })
</script>
</body>

</html>